package com.supermap.business.warn;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.disaster.DisasterTypeService;
import com.supermap.dubbo.api.warn.FileShare;
import com.supermap.dubbo.api.warn.WarnCap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lxs
 * @Title:预警接口
 * @Description:
 * @date 2018/9/29  17:11
 */
@Controller
@RequestMapping("WarnController")
public class WarnController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(WarnController.class);
    @Reference(timeout = 50000)
    private DisasterTypeService disasterTypeService;
    @RequestMapping("/getWarnInfo")
    @ResponseBody
    public JSONUtil getWarnInfo(HttpServletRequest request){
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        String areaCode=request.getParameter("areaCode");
        String itime=request.getParameter("itime");

        String warnAreaCode="";
        try{
            ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            FileShare client = (FileShare)context.getBean("client");
            if(areaCode!=null){
                areaCode=areaCode+"%";
            }
            List<String> warnAreaCodeList = disasterTypeService.getWarnAreaCode(areaCode);
            if(warnAreaCodeList!=null){
                for(String warn:warnAreaCodeList){
                    warnAreaCode=warnAreaCode+warn+",";
                }
            }
            List<WarnCap> customParseFileList = client.listWarnCapByElement(warnAreaCode, "RED,BLUE,YELLOW,YELLOW,UNKOWN", "A", Integer.valueOf(itime));
            jsonUtil.setMsg("getWarnInfo success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(customParseFileList);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("DisasterTypeServiceImpl=>getDisasterType=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("getWarnInfo flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

}
